<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <title>智能跳转中...</title>
        <style>
        /* 基础样式 */
        .container {
            text-align: center;
            padding: 3vw;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell;
            position: relative;
            z-index: 1;
        }

        body {
            margin: 0;
            padding: 0;
            min-height: 100vh;
            background: url('https://totemlife.cn/apps/public/images/solutions/bg-neuro-dev.png') no-repeat center center fixed;
            background-size: cover;
            position: relative;
        }

        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(255, 255, 255, 0.618);
            z-index: 0;
        }

        /* 移动端适配按钮 */
        .redirect-btn {
            padding: 8vw 12vw;
            font-size: 5vw;
            background: #88b04b;
            color: white;
            border: none;
            border-radius: 12px;
            cursor: pointer;
            margin: 2vw auto;
            display: block;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: transform 0.2s, box-shadow 0.2s;
            width: 80%;
            max-width: 320px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        /* 按钮交互效果 */
        .redirect-btn:active {
            transform: scale(0.95);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        /* 按钮容器 */
        .button-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 2vw;
            margin-top: 4vw;
        }

        /* 加载动画 */
        .loader {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #88b04b;
            border-radius: 50%;
            width: 10vw;
            height: 10vw;
            animation: spin 1s linear infinite;
            margin: 4vw auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        /* 移动端媒体查询 */
        @media (max-width: 480px) {
            .redirect-btn {
                padding: 8vw 12vw;
                font-size: 5vw;
                color: white;
                width: 90%;
            }
        }
        </style>
    </head>
    <body>
        <div class="container">
            <div id="autoLoader">
                <p style="margin: 2vw 0; color: #666;">智能跳转中...</p>
                <div class="loader"></div>
                <p style="margin: 2vw 0; color: #666;">点击下方按钮可立即跳转</p>
            </div>

            <div class="button-container">
                <button id="manualBtn" class="redirect-btn" onclick="handleRedirect()">
                    立即打开新内容窗口
                </button>

                <button id="homeBtn" class="redirect-btn" style="background: #1DB0FC;" onclick="handleHomeRedirect()">
                    返回商城首页
                </button>
            </div>
        </div>

        <script>
            const decodedUrl = "{{{decodedUrl}}}?target=ihealthify";

        (function autoRedirect() {
            // 自动跳转尝试
            const autoWindow = window.open('', '_blank');
            if (autoWindow) {
                autoWindow.location.href = decodedUrl;
                document.getElementById('autoLoader').style.display = 'block';
            } else {
                console.log('autoWindow load failed');
            }
        })();

        function handleRedirect() {
            const loader = document.getElementById('autoLoader');
            const btn = document.getElementById('manualBtn');

            // 禁用按钮防止重复点击
            btn.disabled = true;
            loader.style.display = 'block';
            // 创建新窗口
            const newWindow = window.open('', '_blank');
            // 加载目标页面
            newWindow.location.href = decodedUrl;
            const checkWindow = setInterval(() => {
                if (newWindow.closed) {
                    clearInterval(checkWindow);
                    console.log('newWindow closed, back to history');
                    //未来可以考虑在这里主动添加会主页之类的
                }
            }, 1000);
        }

        function handleHomeRedirect() {
            if (window?.wx?.miniProgram) {
                wx.miniProgram.getEnv((res) => {
                    if (res.miniprogram) {
                        wx.miniProgram.switchTab({ url: '/pages/index/index' });
                    } else {
                        window.location.href = 'https://ihealthify.com/';
                    }
                });
            } else {
                window.location.href = 'https://ihealthify.com/';
            }
        }

        </script>
    </body>
</html>
